package io.wongxd.demo

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import io.wongxd.demo.page.*
import io.wongxd.demo.page.emo.FgtEmo
import io.wongxd.demo.page.kvHolder.FgtKvHolder
import io.wongxd.demo.page.mask.FgtMaskSwitch
import io.wongxd.demo.page.mask.MaskActivity
import io.wongxd.solution.baseView.AtyContainer
import io.wongxd.solution.camerax.CameraXHolder
import io.wongxd.solution.compose.composeTheme.*
import io.wongxd.solution.logger.WLogUtil
import io.wongxd.solution.permissionFlow.initializer.PermissionFlowInitializer
import io.wongxd.solution.util.FlowBus.produceEvent
import io.wongxd.solution.util.FlowBus.subscribeEvent
import io.wongxd.solution.util.FlowEvent
import io.wongxd.solution.util.bars.showIme
import io.wongxd.solution.util.bars.showSystemBar

class AtyMain : AppCompatActivity() {

    private val cameraXHolder by lazy { CameraXHolder() }

    private var eventInfo by mutableStateOf("")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AppThemeHolder.themeAdapterAuto = true
        AppThemeHolder.blackTint = true
        AppThemeHolder.appBgColor = Color(0xfff5f5f5)
        PermissionFlowInitializer.init(this)
        WLogUtil.init("滴滴", true)

        subscribeEvent<StepUpdateEvent> {
            eventInfo = "事件信息 StepUpdateEvent :${it.currentStep}"
            WLogUtil.d(eventInfo)
        }

        subscribeEvent<TestEvent> {
            eventInfo = "事件信息 TestEvent :${it.current}"
            WLogUtil.d("事件消息 TestEvent :${it.current}")
        }

        setContent {

//            Box(
//                modifier = Modifier
//                    .fillMaxSize()
//                    .background(AppThemeHolder.colorAssets.white),
//                contentAlignment = Alignment.Center
//            ) {
//
//                CameraXCompose(cameraXHolder = cameraXHolder, openBack = false)
//
//            }

            BoxWithConstraints {

                Column(
                    Modifier
                        .fillMaxSize()
                        .verticalScroll(rememberScrollState()),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {

                    Spacer(modifier = Modifier.height(60.dp))

                    AppBtn(text = "emo") {
                        AtyContainer.startFgt(this@AtyMain, FgtEmo())
                    }

                    val ime = remember {
                        mutableStateOf(false)
                    }

                    AppBtn(text = "showIme") {
                        window.showIme(show = ime.value.not())
                        ime.value = ime.value.not()
                    }

                    val sysUI = remember {
                        mutableStateOf(false)
                    }

                    AppBtn(text = "hideSystemUI") {
                        window.showSystemBar(show = sysUI.value.not())
                        sysUI.value = sysUI.value.not()
                    }

//                    AppBtn(text = "notification") {
//                        startActivity(Intent(this@AtyMain, NotificationActivity::class.java))
//                    }

                    AppBtn(text = "atyBook") {
                        startActivity(Intent(this@AtyMain, BookActivity::class.java))
                    }

                    AppBtn(text = "atyMask") {
                        startActivity(Intent(this@AtyMain, MaskActivity::class.java))
                    }

                    AppBtn(text = "fgtMask") {
                        AtyContainer.startFgt(this@AtyMain, FgtMaskSwitch())
                    }

                    Text(
                        text = eventInfo,
                        style = AppThemeHolder.textStyle.sp14.white(),
                        modifier = Modifier.statusBarsPadding()
                    )

                    Button(onClick = {
                        produceEvent(StepUpdateEvent((System.currentTimeMillis() / 1000).toInt()))
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "发送Step事件")
                    }

                    Button(
                        onClick = {
                            produceEvent(TestEvent((System.currentTimeMillis() / 1000).toInt()))
                        }, modifier = Modifier.padding(top = 20.dp)
                    ) {
                        Text(text = "发送Test事件")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtKvHolder())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtKvHolder")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtPhoto())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtPhoto")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtViewImg())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtViewImg")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtNormal())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtNormal")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtTrans())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtTrans")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtModalSheet())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "ModalSheet")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtTooltip())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "Tooltip")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtEmoModal())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "EmoModal")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtSimplePicker())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "simplePicker")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtDialog())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtDialog")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtCollapsable())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtCollapsable")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtViews())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtViews")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtCache())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtCache")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtLocation())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtLocation")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtPermissionFlow())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtPermissionFlow")
                    }

                    Button(onClick = {
                        AtyContainer.startFgt(this@AtyMain, FgtLog())
                    }, modifier = Modifier.padding(top = 20.dp)) {
                        Text(text = "FgtLog")
                    }

                    Spacer(modifier = Modifier.height(60.dp))
                }

                DebugInfo()
            }
        }

    }
}

class StepUpdateEvent(val currentStep: Int) : FlowEvent(key) {
    companion object {
        const val key: String = "AtyMain.StepUpdateEvent"
    }
}

class TestEvent(val current: Int) : FlowEvent(key) {
    companion object {
        const val key: String = "AtyMain.TestEvent"
    }
}